load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//bazel:flags.bzl", "CPP_FLAGS", "MAPLIBRE_FLAGS")

ICU_FLAGS = [
    "-licuuc ",
    "-licui18n",
    "-licudata",
]

cc_library(
    name = "impl",
    srcs = [
        "src/gl_functions.cpp",
        "src/headless_backend_glx.cpp",
    ],
    copts = CPP_FLAGS + MAPLIBRE_FLAGS,
    linkopts = [
        "-lGL",
        "-lX11",
        "-luv",
        "-lz",
        "-lcurl",
        "-lpng",
        "-ljpeg",
        "-lwebp",
    ] + ICU_FLAGS,
    visibility = ["//visibility:public"],
    deps = [
        "//:mbgl-core",
        "//platform/default:mbgl-default",
    ],
)

cc_static_library(
    name = "maplibre-linux-static",
    visibility = ["//visibility:public"],
    deps = [
        ":impl",
    ],
)

genrule(
    name = "maplibre-linux-amalgam",
    srcs = [":maplibre-linux-static"],
    outs = ["libmaplibre-linux-amalgam.a"],
    cmd_bash = """(
        armerge --keep-symbols "^.+" --output $(OUTS) $(BINDIR)/platform/linux/libmaplibre-linux-static.a \
            $$(pkg-config --variable=libdir icu-uc)/libicuuc.a \
            $$(pkg-config --variable=libdir icu-uc)/libicudata.a \
            $$(pkg-config --variable=libdir icu-i18n)/libicui18n.a
    )""",
)

sh_binary(
    name = "startxwrapper",
    srcs = ["xorg/startxwrapper"],
)
